package com.controller;

import com.domain.Employee;
import com.dao.EmployeeDao;
import com.server.EmployeeService;
import com.domain.Whour;
import com.dao.WhourDao;
import com.server.WhourService;
import com.domain.Team;
import com.dao.TeamDao;
import com.server.TeamService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/index")
public class LoginController {

    @Resource(name = "employeeService")
    private EmployeeService employeeService;

    @Resource(name = "whourService")
    private WhourService whourService;

    @Resource(name = "teamService")
    private TeamService teamService;

    @RequestMapping(value = "/login")
    public String login(){
        return "01login";
    }

    @RequestMapping(value="/loginEnter",method = RequestMethod.POST)
    public String loginEnter(String number, String password,
                             Model model, HttpSession session){
        System.out.println("用户进行登录操作");
        if ((!number.equals("") && number != null)
                && (!password.equals("") && password != null)) {
            // 将用户输入的用户名和密码于数据库中查找的结果对比，判断存在该用户：
            Employee employee=employeeService.findLoginEmployee(number,password);
            session.setAttribute("employee", employee);
            model.addAttribute("employee",employee);
            if(employee!=null){
                List<Whour> whourList=whourService.findListWhourByEmployeeNumber(number);
                session.setAttribute("whourList",whourList);
                model.addAttribute("whourList",whourList);
                List<Team> teamList=new ArrayList();
                for(Whour whour:whourList){
                    //得到团队信息
                    int team_id=whour.getTeam_id();
                    Team team=teamService.findNameByTeamId(team_id);
                    teamList.add(team);
                }
                session.setAttribute("teamList",teamList);
                model.addAttribute("teamList",teamList);
                System.out.println("****登入成功****");
                return "02main";
            }else {
                model.addAttribute("message", "登录失败，该用户不存在，请重新输入！");
                System.out.println("****登入失败****");
                return "01login";
            }
        } else {
            return "01login";
        }
    }

    @RequestMapping(value = "/signout")
    public String signout(HttpSession session){
        session.invalidate();//使用此方法可以清除Session对象中的所有信息
        return "01login";
    }

}
